﻿@{
    ViewBag.Title = ViewBag.CurrentMenuName;
    Layout = "~/Views/Shared/_AdminLayout.cshtml";
    var lstApp = ViewBag.lstApp;
    var lstParentMenu = ViewBag.lstParentMenu;

    SelectList slState = new SelectList(new[] {
        new SelectListItem { Value = "2", Text = "Tất cả"},
        new SelectListItem { Value = "1", Text = "Đang sử dụng"},
        new SelectListItem { Value = "0", Text = "Không sử dụng"}
    }, "Value", "Text", 1);
}

<div class="content">
    <div class="clear"></div>
    <table style="width: 100%">
        <tr>
            <td style="height: 40px;">
                <div id="nav"><a href="/Menu/Index" title="">@ViewBag.Title.ToUpper()</a> </div>
            </td>
        </tr>
        <tr>
            <td colspan="4">
                <div style="width: 100%;">
                    <div>
                        <div>
                            <div class="col-md-2" style="text-align:right;">
                                <label>@Resources.Localization.AppName</label>
                            </div>
                            <div class="col-md-3">
                                @Html.DropDownList("appid", new SelectList(ViewBag.lstApp, "value", "text", 0), new { @id = "input-appid", @name = "appid", @class = "SelectBoxContent", @placeholder = "" })
                            </div>
                        </div>
                        <div class="col-md-1" style="text-align:right;">
                            <label>@Resources.Localization.MenuName</label>
                        </div>
                        <div class="col-md-3">
                            <input type="text" class="txtsearch TextBoxContent" style="width: 100% !important" id="input-menuname" name="menuname">
                        </div>
                        <div class="col-md-1" style="text-align:right;">
                            <label>@Resources.Localization.MenuCode</label>
                        </div>
                        <div class="col-md-2">
                            <input type="text" class="txtsearch TextBoxContent" style="width: 100% !important" id="input-menucode" name="menucode">
                        </div>
                        
                    </div>
                </div>
            </td>
        </tr>
        <tr>
            <td colspan="4">
                <div style="width: 100%;">
                    <div class="col-md-2" style="text-align: right; padding: 4px; font-size:14px;">
                        Chức năng cha
                    </div>
                    <div class="col-md-3">
                        @Html.DropDownList("parent-menu", new SelectList(ViewBag.lstParentMenu, "value", "text", 0), null, new { @class = "TextBoxContent", @style = "width: 100% !important" })
                    </div>
                    <div class="col-md-1" style="text-align: right; padding: 4px; font-size:14px;">
                        Trạng thái
                    </div>
                    <div class="col-md-3">
                        @Html.DropDownList("input-state", slState, null, new { @class = "TextBoxContent", @style = "width: 100% !important" })
                    </div>
                    @*<div class="col-md-3">
                        <span class="input-group-btn">
                            <button class="btn btn-primary" type="button" onclick="return btnSearchClick();">
                                <span class="glyphicon glyphicon-search"></span>Tìm kiếm
                            </button>

                            <button type="button" id="menu_add" style="margin-left: 10px;" class="btn btn-primary" value="Thêm mới">
                                <span class="glyphicon glyphicon-plus"></span>Thêm chức năng
                            </button>

                        </span>
                    </div>*@
                </div>
            </td>
        </tr>
        <tr>
            <td colspan="4">
                <div>
                    <div style="height: 10px;"></div>
                    <div class="col-md-12">
                        <div style="padding-top: 10px; text-align: center">
                            <div class="col-md-12">
                                <span class="input-group-btn">
                                    <button class="btn btn-primary" type="button" onclick="return btnSearchClick();">
                                        <span class="glyphicon glyphicon-search"></span>Tìm kiếm
                                    </button>

                                    <button type="button" id="menu_add" style="margin-left: 10px;" class="btn btn-primary" value="Thêm mới">
                                        <span class="glyphicon glyphicon-plus"></span>Thêm chức năng
                                    </button>

                                </span>
                            </div>
                        </div>
                    </div>
                </div>

            </td>
        </tr>
        <tr>
            <td colspan="4">
                <div id="menu-list" style="margin-top:10px;">
                    @{
                        Html.RenderPartial("_MenuList");
                    }
                </div>
            </td>
        </tr>
    </table>
</div>

<script>
    @functions{
        public string GetAntiForgeryToken()
        {
            string cookieToken, formToken;
            AntiForgery.GetTokens(null, out cookieToken, out formToken);
            return cookieToken + "," + formToken;
        }
    }

    function btnSearchClick() {
        OnCriteria(0);
    }

    

    $(document).ready(function () {
        $('#menu_add').bind('click', function (event) {
            prepareCreateOrUpdate(0, 'Thêm mới Chức năng');
        });
        $('#input-state').change(function () {
            OnCriteria(0);
        });

        $('#input-appid').change(function () {
            var appid = $('#input-appid').val();
            $.ajax({
                dataType: "json",
                type: "POST",
                url: '/Menu/GetMenuByParentID',
                contentType: "application/json",
                data: JSON.stringify({ strAppID: appid }),
                success: function (data) {
                    var result = '';
                    for (var i = 0; i < data.length; i++) {
                        result += '<option value="' + data[i].Value + '">' + data[i].Text + '</option>';
                    }
                    $("#parent-menu").html(result);
                    //OnCriteria(0);
                }
            });
        });
        //$('#input-appid').change(function () {
        //    OnCriteria(0);
        //});
    });

    function prepareCreateOrUpdate(menuId, dlgTitle) {
        $('#menu-model-container')
              .load("@Url.Action("actionPrepareCreateOrUpdateMenu", "Menu")", { menuId: menuId }, function () {
                  $(this).dialog({
                      draggable: true,
                      modal: true,
                      resizable: false,
                      width: 'auto',
                      title: dlgTitle,
                      minHeight: 75,
                      my: "center",
                      at: "center",
                      of: window,
                      buttons: [
                            {
                                text: "Lưu",
                                icons: {
                                    primary: "ui-icon-heart"
                                },
                                class: 'btn btn-primary',
                                click: function () {
                                    if ($('#MenuName').val() == "" || $('#MenuCode').val() == "" || $('#MenuUrl').val() == "") {
                                        if (save) {
                                            var result = save();
                                            if (result) {
                                                $(this).dialog("destroy");
                                                $('#menu-model-container').hide();
                                            }
                                        }
                                    }
                                    else {
                                        var cName = checkName()
                                        var cCode = checkCode();
                                        var cMenuUrl = checkMenuUrl();
                                        if (cName == false || cCode == false || cMenuUrl == false)
                                            return;
                                        else {
                                            if (save) {
                                                var result = save();
                                                if (result) {
                                                    $(this).dialog("destroy");
                                                    $('#menu-model-container').hide();
                                                }
                                            }
                                        }
                                    };
                                }
                            },
                            {
                                text: "Đóng",
                                icons: {
                                    primary: "ui-icon-close"
                                },
                                class: 'btn btn-primary',
                                click: function () {
                                    $(this).dialog("destroy");
                                    $('#menu-model-container').hide();
                                }

                            }
                      ]
                  });
              });
    }
</script>